package org.vectrics.mail.struts;

import java.io.File;
import java.util.Date;

import org.apache.log4j.Logger;
import org.vectrics.SystemException;
import org.vectrics.common.util.DateSync;
import org.vectrics.common.util.FileUtil;
import org.vectrics.config.MailConfiguration;
import org.vectrics.domain.Domain;
import org.vectrics.hibernate.ThreadProperties;
import org.vectrics.mail.MailMessage;
import org.vectrics.mail.UserMailer;
import org.vectrics.provider.CoreServiceLocator;
import org.vectrics.user.Person;
import org.vectrics.user.PersonCommunication;
import org.vectrics.user.PersonCommunicationMethod;
import org.vectrics.user.PersonCommunicationMethodType;
import org.vectrics.user.PersonEncounterStatusType;

public class SendRecipientLetterAction extends RecipientLetterAction {
	private Logger log = Logger.getLogger(this.getClass());
	private UserMailer mailer = null;
	private Domain domain = null;
	private Person sender = null;
	private LetterStagingManager letterStagingManager = null;
	
	public SendRecipientLetterAction(LetterStagingManager stagingManager, Person sender) {
		super();
		domain = ThreadProperties.getDomain();
		MailConfiguration domainMailConfiguration = CoreServiceLocator.getConfiguration().getFromMailConfiguration(domain);
		mailer = new UserMailer(domainMailConfiguration);
		this.sender = sender;
		this.letterStagingManager = stagingManager;
	}

	@Override
	public void execute(Person recipient, File letterFile) {
		log.info("emailing letter to: " + recipient);
		String subject = this.letterStagingManager.getSubject(recipient);
		MailMessage mailMessage = new MailMessage(domain);
		String contents = FileUtil.getFileContents(letterFile.getAbsolutePath());
		mailMessage.setBody(contents);
		mailMessage.setContentType(MailMessage.CONTENT_TEXT);
		mailMessage.setFromName(sender.getFullName());
		mailMessage.setFromAddress(sender.getContactInfo().getEmail());
		mailMessage.setSubject(subject);
		mailer.sendMessage(recipient, mailMessage);
		
		Date now = DateSync.getCurrentTime();
		PersonCommunication communication = new PersonCommunication();
		communication.setDateStart(now);
		communication.setDateEnd(now);
		communication.setDirection(PersonCommunication.DIRECTION_TO);
		communication.setLeadPerson(sender);
		communication.setPerson(recipient);
		PersonEncounterStatusType doneStatus = CoreServiceLocator.getPersonService().findPersonEncounterStatusType(PersonEncounterStatusType.STATUS_DONE);
		communication.setEncounterStatus(doneStatus);
		PersonCommunicationMethodType methodType = CoreServiceLocator.getPersonService()
			.findPersonCommunicationMethodType(PersonCommunicationMethodType.TYPE_EMAIL);
		if (methodType == null)
			throw new SystemException("Could not find communication method");
		PersonCommunicationMethod method = new PersonCommunicationMethod();
		method.setPersonCommunicationMethodType(methodType);
		communication.setMethod(method);
		CoreServiceLocator.getPersonService().updatePersonCommunication(communication);
	}

}
